package com.zyd.shiro.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyd.shiro.business.entity.Repair;
import com.zyd.shiro.business.service.PakRepairService;
import com.zyd.shiro.framework.object.BaseConditionVO;
import com.zyd.shiro.persistence.beans.PakRepair;
import com.zyd.shiro.persistence.mapper.PakRepairMapper;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Slf4j
@org.springframework.stereotype.Service
public class PakRepairServiceImpl implements PakRepairService {

    @Autowired
    private PakRepairMapper pakRepairMapper;

    @Override
    public Repair insert(Repair repair) {
        Assert.notNull(repair, "对象不可为空！");
        repair.getPakRepair().setUnicode("PF"+System.currentTimeMillis());
        repair.getPakRepair().setAppstatus(0);
        repair.getPakRepair().setSerstatus(0);
        repair.getPakRepair().setUpdateTime(new Date());
        repair.getPakRepair().setCreateTime(new Date());
        pakRepairMapper.insertSelective(repair.getPakRepair());
        return repair;
    }

    @Override
    public void insertList(List<Repair> repairs) {
            for(Repair repair:repairs){
                repair.getPakRepair().setUpdateTime(new Date());
                repair.getPakRepair().setCreateTime(new Date());
                pakRepairMapper.insertSelective(repair.getPakRepair());
           }
    }


    /**
     * 根据主键字段进行删除，方法参数必须包含完整的主键属性
     *
     * @param primaryKey
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByPrimaryKey(Long primaryKey) {
        return pakRepairMapper.deleteByPrimaryKey(primaryKey) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(Repair repair) {
        Assert.notNull(repair, "对象不可为空！");
        repair.setUpdateTime(new Date());
        return pakRepairMapper.updateByPrimaryKey(repair.getPakRepair()) > 0;
    }

    @Override
    public boolean updateSelective(Repair repair) {
        Assert.notNull(repair, "对象不可为空！");
        repair.setUpdateTime(new Date());
        return pakRepairMapper.updateByPrimaryKeySelective(repair.getPakRepair()) > 0;
    }

    /**
     * 根据主键字段进行查询，方法参数必须包含完整的主键属性，查询条件使用等号
     *
     * @param primaryKey
     * @return
     */

    @Override
    public Repair getByPrimaryKey(Long primaryKey) {
        PakRepair pakRepair = pakRepairMapper.selectByPrimaryKey(primaryKey);
        return null == pakRepair ? null : new Repair(pakRepair);
    }


    /**
     * 根据实体中的属性进行查询，只能有一个返回值，有多个结果时抛出异常，查询条件使用等号
     *
     * @param
     * @return
     */
    @Override
    public Repair getOneByEntity(Repair repair) {
        Assert.notNull(repair, "对象不可为空！");
        PakRepair pakRepair = pakRepairMapper.selectOne(repair.getPakRepair());
        return null == pakRepair ? null : new Repair(pakRepair);
    }

    @Override
    public List<Repair> listAll() {
        List<PakRepair> pakRepairs = pakRepairMapper.selectAll();
        if (CollectionUtils.isEmpty(pakRepairs)) {
            return null;
        }
        List<Repair> repairs = new ArrayList<>();
        for (PakRepair pakRepair : pakRepairs) {
            repairs.add(new Repair(pakRepair));
        }
        return repairs;
    }

    @Override
    public List<Repair> listByEntity(Repair entity) {
        return null;
    }


    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @Override
    public PageInfo<Repair> findPageBreakByCondition(BaseConditionVO vo) {
        PageHelper.startPage(vo.getPageNumber(), vo.getPageSize());
        List<PakRepair> pakRepairs = pakRepairMapper.findPageBreakByCondition(vo);
        if (CollectionUtils.isEmpty(pakRepairs)) {
            return null;
        }
        List<Repair> repairs = new ArrayList<>();
        for (PakRepair pakService : pakRepairs) {
            repairs.add(new Repair(pakService));
        }
        PageInfo bean = new PageInfo<PakRepair>(pakRepairs);
        bean.setList(repairs);
        return bean;
    }
@Override
    public void increase(PakRepair pakRepair) {
    pakRepairMapper.insert(pakRepair);
    }




}
